/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void f(TreeNode* root, int deep, vector<int>& ans)
    {
        if(root == nullptr) return;

        if(ans.size() == deep)
            ans.emplace_back(root->val);
        f(root->right, deep + 1, ans);
        f(root->left, deep + 1, ans);
    }

    vector<int> rightSideView(TreeNode* root) {
        vector<int> ret;
        f(root, 0, ret);
        return ret;
    }

};
